<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML课件直链托管小站</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .upload-area {
            border: 2px dashed #cbd5e0;
            transition: all 0.3s ease;
        }
        .upload-area:hover {
            border-color: #667eea;
            background-color: #f7fafc;
        }
        .upload-area.dragover {
            border-color: #667eea;
            background-color: #edf2f7;
        }
    </style>
</head>
<body class="flex items-center justify-center p-4">
    <div class="max-w-2xl w-full">
        <div class="bg-white rounded-lg shadow-2xl p-8">
            <div class="text-center mb-8">
                <h1 class="text-3xl font-bold text-gray-800 mb-2">
                    <i class="fas fa-cloud-upload-alt text-blue-500 mr-2"></i>
                    HTML课件直链托管
                </h1>
                <p class="text-gray-600">上传HTML文件，生成可直接访问的直链</p>
            </div>

            <!-- 上传区域 -->
            <div class="mb-6">
                <div id="uploadArea" class="upload-area rounded-lg p-8 text-center cursor-pointer">
                    <i class="fas fa-file-code text-4xl text-gray-400 mb-4"></i>
                    <h3 class="text-lg font-semibold text-gray-700 mb-2">拖拽或点击上传HTML文件</h3>
                    <p class="text-sm text-gray-500 mb-4">支持 .html 格式，最大10MB</p>
                    <input type="file" id="fileInput" accept=".html" class="hidden">
                    <button id="uploadBtn" class="bg-blue-500 hover:bg-blue-600 text-white px-6 py-2 rounded-lg transition">
                        <i class="fas fa-upload mr-2"></i>选择文件
                    </button>
                </div>
            </div>

            <!-- 上传进度 -->
            <div id="uploadProgress" class="hidden mb-4">
                <div class="bg-gray-200 rounded-full h-2">
                    <div id="progressBar" class="bg-blue-500 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
                </div>
                <p id="progressText" class="text-sm text-gray-600 mt-2 text-center">上传中...</p>
            </div>

            <!-- 结果展示 -->
            <div id="resultArea" class="hidden">
                <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-4">
                    <h4 class="text-green-800 font-semibold mb-2">
                        <i class="fas fa-check-circle mr-2"></i>上传成功！
                    </h4>
                    <div class="space-y-2">
                        <div>
                            <label class="text-sm font-medium text-gray-700">文件名：</label>
                            <span id="fileName" class="text-sm text-gray-600"></span>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-700">直链地址：</label>
                            <div class="flex items-center space-x-2 mt-1">
                                <input type="text" id="fileUrl" readonly 
                                       class="flex-1 px-3 py-2 border border-gray-300 rounded-md text-sm bg-gray-50">
                                <button id="copyBtn" class="bg-gray-500 hover:bg-gray-600 text-white px-3 py-2 rounded-md text-sm">
                                    <i class="fas fa-copy"></i>
                                </button>
                            </div>
                        </div>
                        <div>
                            <a id="previewLink" href="#" target="_blank" 
                               class="inline-flex items-center text-blue-600 hover:text-blue-800 text-sm">
                                <i class="fas fa-external-link-alt mr-1"></i>在新窗口打开
                            </a>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 文件列表 -->
            <div id="fileList" class="hidden">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">
                    <i class="fas fa-list mr-2"></i>已上传的文件
                </h3>
                <div id="filesContainer" class="space-y-2 max-h-64 overflow-y-auto">
                    <!-- 文件列表将在这里动态生成 -->
                </div>
            </div>
        </div>
    </div>

    <script>
        // 获取DOM元素
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const uploadProgress = document.getElementById('uploadProgress');
        const progressBar = document.getElementById('progressBar');
        const progressText = document.getElementById('progressText');
        const resultArea = document.getElementById('resultArea');
        const fileName = document.getElementById('fileName');
        const fileUrl = document.getElementById('fileUrl');
        const copyBtn = document.getElementById('copyBtn');
        const previewLink = document.getElementById('previewLink');
        const fileList = document.getElementById('fileList');
        const filesContainer = document.getElementById('filesContainer');

        // 事件监听器
        uploadBtn.addEventListener('click', () => fileInput.click());
        fileInput.addEventListener('change', handleFileSelect);
        
        // 拖拽上传
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });
        
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });
        
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                uploadFile(files[0]);
            }
        });

        // 处理文件选择
        function handleFileSelect(e) {
            const file = e.target.files[0];
            if (file) {
                uploadFile(file);
            }
        }

        // 上传文件
        async function uploadFile(file) {
            if (!file.name.endsWith('.html')) {
                alert('请上传HTML文件！');
                return;
            }

            const formData = new FormData();
            formData.append('htmlFile', file);

            // 显示上传进度
            uploadProgress.classList.remove('hidden');
            resultArea.classList.add('hidden');
            
            try {
                const xhr = new XMLHttpRequest();
                
                // 监听上传进度
                xhr.upload.addEventListener('progress', (e) => {
                    if (e.lengthComputable) {
                        const percentComplete = (e.loaded / e.total) * 100;
                        progressBar.style.width = percentComplete + '%';
                        progressText.textContent = `上传中... ${Math.round(percentComplete)}%`;
                    }
                });

                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4) {
                        uploadProgress.classList.add('hidden');
                        
                        if (xhr.status === 200) {
                            const response = JSON.parse(xhr.responseText);
                            showResult(response);
                            loadFileList();
                        } else {
                            const error = JSON.parse(xhr.responseText);
                            alert('上传失败：' + error.error);
                        }
                    }
                };

                xhr.open('POST', '/upload');
                xhr.send(formData);
                
            } catch (error) {
                uploadProgress.classList.add('hidden');
                alert('上传失败：' + error.message);
            }
        }

        // 显示上传结果
        function showResult(response) {
            fileName.textContent = response.filename;
            fileUrl.value = response.fullUrl;
            previewLink.href = response.url;
            resultArea.classList.remove('hidden');
        }

        // 复制链接
        copyBtn.addEventListener('click', () => {
            fileUrl.select();
            document.execCommand('copy');
            
            const originalText = copyBtn.innerHTML;
            copyBtn.innerHTML = '<i class="fas fa-check"></i>';
            setTimeout(() => {
                copyBtn.innerHTML = originalText;
            }, 2000);
        });

        // 加载文件列表
        async function loadFileList() {
            try {
                const response = await fetch('/files');
                const files = await response.json();
                
                if (files.length > 0) {
                    fileList.classList.remove('hidden');
                    filesContainer.innerHTML = '';
                    
                    files.forEach(file => {
                        const fileItem = document.createElement('div');
                        fileItem.className = 'flex items-center justify-between p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition';
                        fileItem.innerHTML = `
                            <div class="flex-1 min-w-0">
                                <p class="text-sm font-medium text-gray-900 truncate">${file.name}</p>
                                <p class="text-xs text-gray-500">${new Date(file.uploadTime).toLocaleString()}</p>
                            </div>
                            <div class="flex items-center space-x-2 ml-4">
                                <a href="${file.url}" target="_blank" class="text-blue-600 hover:text-blue-800">
                                    <i class="fas fa-external-link-alt"></i>
                                </a>
                                <button onclick="copyToClipboard('${file.fullUrl}')" class="text-gray-600 hover:text-gray-800">
                                    <i class="fas fa-copy"></i>
                                </button>
                            </div>
                        `;
                        filesContainer.appendChild(fileItem);
                    });
                }
            } catch (error) {
                console.error('加载文件列表失败:', error);
            }
        }

        // 复制到剪贴板
        function copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                // 显示复制成功提示
                const toast = document.createElement('div');
                toast.className = 'fixed top-4 right-4 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg';
                toast.textContent = '链接已复制到剪贴板';
                document.body.appendChild(toast);
                
                setTimeout(() => {
                    toast.remove();
                }, 2000);
            });
        }

        // 页面加载时加载文件列表
        loadFileList();
    </script>
</body>
</html>
